package com.cityfood.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cityfood.mapper.CfCategoryMapper;
import com.cityfood.pojo.CfCategory;
import com.cityfood.pojo.CfCategoryExample;
import com.cityfood.pojo.CfUser;
import com.cityfood.pojo.CfCategoryExample.Criteria;
import com.cityfood.service.CategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class CategoryServiceImpl implements CategoryService {
	
	@Autowired
	private CfCategoryMapper categoryMapper;
	
	@Override
	public List<CfCategory> getCategoryByParentId(Integer parentId) {
		CfCategoryExample example = new CfCategoryExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdEqualTo(parentId);
		List<CfCategory> list = categoryMapper.selectByExample(example);
		return list;
	}
	
	@Override
	public List<CfCategory> getCategoryLikeName(String categoryName) {
		CfCategoryExample example = new CfCategoryExample();
		Criteria criteria = example.createCriteria();
		criteria.andNameLike("%"+categoryName+"%");
		List<CfCategory> list = categoryMapper.selectByExample(example);
		return list;
	}

	@Override
	public List<CfCategory> getCategoryByCityId(Integer cityId) {
		CfCategoryExample example = new CfCategoryExample();
		Criteria criteria = example.createCriteria();
		criteria.andCityIdEqualTo(cityId);
		List<CfCategory> list = categoryMapper.selectByExample(example);
		return list;
	}
	
	@Override
	public PageInfo<CfCategory> getCategoryByCityIdAndPage(Integer cityId,Integer pageNum) {
		//设置分页信息
		PageHelper.startPage(pageNum, 20);
		
		CfCategoryExample example = new CfCategoryExample();
		
		if(cityId != 0){  //ID为0的时候查全部
			Criteria criteria = example.createCriteria();
			criteria.andCityIdEqualTo(cityId);
		}
		List<CfCategory> list = categoryMapper.selectByExample(example);
		
		//5.获取分页信息
		PageInfo<CfCategory> info = new PageInfo<>(list);
				
		return info;
	}

	@Override
	public int insertCategory(CfCategory category) {
		category.setCreateTime(new Date());
		category.setUpdateTime(new Date());
		int i = categoryMapper.insertSelective(category);
		return i;
	}

	@Override
	public int updateCategory(CfCategory category) {
		category.setUpdateTime(new Date());
		int i = categoryMapper.updateByPrimaryKeySelective(category);
		return i;
	}

	@Override
	public int deleteCategory(CfCategory category) {
		if((byte)1 == category.getStatus()){
			category.setStatus((byte)0);
		}else if ((byte) 0 == category.getStatus()){
			category.setStatus((byte)1);
		}
		category.setUpdateTime(new Date());
		int i = categoryMapper.updateByPrimaryKeySelective(category);
		return i;
	}

	@Override
	public List<CfCategory> getallCategorys() {
		CfCategoryExample example = new CfCategoryExample();
		List<CfCategory> list = categoryMapper.selectByExample(example);
		return list;
	}

}
